/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.commons;

import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import cn.ac.iie.di.ban.config.Configuration;
import cn.ac.iie.di.ban.metastore.MetaStoreManager;

public class RuntimeEnv {

    private static org.apache.log4j.Logger logger = Logger.getLogger(RuntimeEnv.class.getName());
    public static MetaStoreManager metaStoreManager = null;
    private static Configuration conf = null;

    public static final String DATA_CACHE_SIZE = "dataCacheSize";
    public static final String TIMEOUT = "timeout";
    public static final String WAIT_TIME = "waitTime";
    public static final String MYSQL_COMMON = "mysql_common";
    public static final String MYSQL_CONF = "mysql_conf";
    public static final String GET_DB_COLUMNS = "get_db_columns";
    public static final String SERVER_CONF = "server_conf";
    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();

    public static boolean initialize(Configuration pConf) throws Exception {
        if (pConf == null) {
            logger.error("configuration object is null");
            return false;
        }
        conf = pConf;
        getConfAndSaveAsInteger(DATA_CACHE_SIZE);
        getConfAndSaveAsInteger(TIMEOUT);
        getConfAndSaveAsInteger(WAIT_TIME);
        getConfAndSaveAsString(MYSQL_COMMON,false);
        getConfAndSaveAsString(SERVER_CONF,false);
        getConfAndSaveAsString(MYSQL_CONF, false);
        getConfAndSaveAsString(GET_DB_COLUMNS, true);
        metaStoreManager = MetaStoreManager.getMetaStoreManger();
        if (metaStoreManager == null) {
            return false;
        }else{
            metaStoreManager.init();
        }
        return true;
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }

    public static boolean getConfAndSaveAsString(String confName, Boolean isempty) {
        String tmp = conf.getString(confName, "");
        if (!isempty) {
            if (tmp.isEmpty()) {
                logger.error("parameter" + confName + "does not exist or is not defined");
                return false;
            }
        }
        dynamicParams.put(confName, tmp);
        logger.info(confName + ":" + tmp);
        return true;
    }

    public static boolean getConfAndSaveAsInteger(String confName) {
        String tmp = conf.getString(confName, "");
        if (tmp.isEmpty()) {
            logger.error("parameter " + confName + " does not exist or is not defined");
            return false;
        }
        try {
            dynamicParams.put(confName, Integer.parseInt(tmp));
            logger.info(confName + ":" + tmp);
        } catch (Exception ex) {
            logger.error("parameter " + confName + " 's format is wrong for " + ex.getMessage(), ex);
            return false;
        }
        return true;
    }

    public static boolean getConfAndSaveAsString(String confName) {
        String tmp = conf.getString(confName, "");
        if (tmp.isEmpty()) {
            logger.error("parameter" + confName + "does not exist or is not defined");
            return false;
        }
        dynamicParams.put(confName, tmp);
        logger.info(confName + ":" + tmp);
        return true;
    }

    public static boolean getConfAndSaveAsBoolean(String confName) {
        String tmp = conf.getString(confName, "");
        if (tmp.isEmpty()) {
            logger.error("parameter " + confName + " does not exist or is not defined");
            return false;
        }
        try {
            dynamicParams.put(confName, Boolean.parseBoolean(tmp));
            logger.info(confName + ":" + tmp);
        } catch (Exception ex) {
            logger.error("parameter " + confName + " 's format is wrong for " + ex.getMessage(), ex);
            return false;
        }
        return true;
    }

    public static String getParamAsString(String pParamName) {
        return (String) dynamicParams.get(pParamName);
    }

    public static Integer getParamAsInteger(String pParamName) {
        return (Integer) dynamicParams.get(pParamName);
    }

    public static Boolean getParamAsBoolean(String pParamName) {
        return (Boolean) dynamicParams.get(pParamName);
    }
}
